package com.buwstudio.domain.uuid;

import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Transient;

import org.springframework.data.domain.Persistable;

import com.buwstudio.domain.AbstractDomain;
import com.buwstudio.domain.utils.IDGeneratorUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import lombok.Data;
import lombok.EqualsAndHashCode;


/**
 * UUID主键的抽象领域模型聚合根基类，
 * 此基类中UUID即为可以是物理标识 ，又可以扮演业务标识
 * 
 * 在服务层创建uuid主键的domain时的流程：
 * 1. 调用initUUID()
 * 2. 调用isValid()
 * 3. 调用registerEvent()
 * 4. 调用save()
 */
@JsonIgnoreProperties({"domainEvents"})
@Data
@EqualsAndHashCode(callSuper=false, of = "id")
@MappedSuperclass
public abstract class DomainEntity extends AbstractDomain implements Persistable<String>{

	private static final long serialVersionUID = -2900351526026967437L;

	/**
	 * 领域模型 物理标识
	 */
	@Id
	@Column(name = "id", unique=true, updatable = false, nullable = false, columnDefinition="char(36)")
	private String id;
	
	public void initUUID() {
		this.setId(IDGeneratorUtil.getUUID());	
	}
	
    @JsonIgnore
	@Transient
	public boolean isNew() {
		return this.id == null;
	}
    
	@Override
	public String toString() {
		return String.format("Entity of type %s with id: %s", this.getClass().getName(), getId());
	}

}
